<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'helpvideo:helpCenterInfo:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.moduleType }}
        </a>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" v-auth="'helpvideo:helpCenterInfo:edit'" type="text">
          <span style="font-size:14px; color: #52c41a;">编辑</span>
        </a-button>
        <Popconfirm
          title="是否确认删除帮助中心视频表"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
          @cancel="handleCancel"
        >
          <a-button v-auth="'helpvideo:helpCenterInfo:edit'" type="text" style="color: red;">
            <span style="font-size: 14px;">删除</span>
          </a-button>
        </Popconfirm>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsHelpvideoHelpCenterInfoList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { helpCenterInfoDelete, helpCenterInfoListData } from '/@/api/helpCenter/helpCenter';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件

  const { t } = useI18n('helpvideo.helpCenterInfo');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('帮助中心'),
  };

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t('模块对象'),
        field:'moduleType',
        component: 'Input',
      },
      {
        label: t('标题'),
        field: 'title',
        component: 'Input',
      },
      {
        label: t('描述内容'),
        field: 'description',
        component: 'Input',
      },
      {
        label: t('视频地址'),
        field: 'videoUrl',
        component: 'Input',
      },
      {
        label: t('创建时间'),
        field: 'createTime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('模块对象'),
      dataIndex:'moduleType',
      key: 'a.module_type',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('标题'),
      dataIndex: 'title',
      key: 'a.title',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('描述内容'),
      dataIndex: 'description',
      key: 'a.description',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('视频地址'),
      dataIndex: 'videoUrl',
      key: 'a.video_url',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createTime',
      key: 'a.create_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('是否删除'),
      dataIndex: 'deleted',
      key: 'a.deleted',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('操作'),
      dataIndex: 'action',
      key: 'action',
      width: 160,
      fixed: 'right',
      slot: 'action' // 指定使用 action 插槽
    }
  ];

  const [registerTable, { reload }] = useTable({
    api: helpCenterInfoListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn, // 移除原有的 actionColumn 配置
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await helpCenterInfoDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  function handleCancel() {
    console.log('取消删除操作');
  }
</script>